package org.xdj.blue.java.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServlet implements Runnable {
	private int port;
	
	public NettyServlet(int port) {
		this.port = port;
	}

	public void run() {
		System.out.println("--monitor"+port+"启动--");
		EventLoopGroup boss = new NioEventLoopGroup();
		EventLoopGroup work = new NioEventLoopGroup();
		ServerBootstrap server = new ServerBootstrap();
		try {
			server.group(boss, work)
			.channel(NioServerSocketChannel.class)
			.option(ChannelOption.SO_BACKLOG, 1024)
			.childHandler(new HelloChannelInitializer());
			ChannelFuture f = server.bind(port).sync();
			f.channel().closeFuture().sync();
		} catch (Exception e) {
			System.out.println("--error:MyServer.run--");
		}
		finally{
			boss.shutdownGracefully();
			work.shutdownGracefully();
		}

	}

}
